package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.zzyl.dto.BedDto;
import com.zzyl.entity.Bed;
import com.zzyl.mapper.BedMapper;
import com.zzyl.service.BedService;
import com.zzyl.utils.ObjectUtil;
import com.zzyl.vo.BedVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class BedServiceImpl implements BedService {

    @Autowired
    private BedMapper bedMapper;

    @Override
    public List<BedVo> getBedsByRoomId(Long roomId) {
        return bedMapper.getBedsByRoomId(roomId);
    }

    @Override
    public BedVo selectById(long id) {
        //根据ID查询床位
        Bed bed = bedMapper.selectBedById(id);
        return BeanUtil.toBean(bed,BedVo.class);
    }

    /**
     * 新增床位
     *
     * @param bedDto
     */
    @Override
    public void addBed(BedDto bedDto) {
        //对象拷贝
        Bed bed = BeanUtil.toBean(bedDto, Bed.class);
        //创建人
        bed.setCreateBy(1L);
        //床位状态
        bed.setBedStatus(0);

        bed.setUpdateTime(LocalDateTime.now());
        //创建时间
        bed.setCreateTime(LocalDateTime.now());
        bedMapper.addBed(bed);
    }

    /**
     * 根据ID修改床位
     * @param bedDto
     */
    @Override
    public void updateById(BedDto bedDto) {
        //对象拷贝
        Bed bed = BeanUtil.toBean(bedDto, Bed.class);
        //修改时间
        bed.setUpdateTime(LocalDateTime.now());
        //修改人ID
        bed.setUpdateBy(1L);
        //床位状态
        if (ObjectUtil.isNotEmpty(bedDto.getBedStatus())){
            bed.setSort(bedDto.getBedStatus());
        }
    }

    /**
     * 删除床位
     *
     * @param id
     */
    @Override
    public void delById(long id) {
        bedMapper.delById(id);
    }

}

